# from model.User_model import UserModel
# from public.db import db_session
# from manager import app


# class UserMapper:
#
#     def save(self, user_body: UserModel):  # user表添加
#         with app.app_context():
#             db_session.add(user_body)
#             db_session.commit()
#
#     def get(self, id) -> UserModel:
#         with app.app_context():
#             return db_session.query(UserModel).filter_by(id=id).first()
#
#     def get_all(self):
#         # return db_session.query(TestcaseDo).all()
#         with app.app_context():
#             return UserModel.query.all()
#
#     def delete(self, id):
#         with app.app_context():
#             db_session.query(UserModel).filter_by(id=id).delete()
#             db_session.commit()
#
#     def update(self, id, update_data):
#         with app.app_context():
#             db_session.query(UserModel).filter_by(id=id).update(update_data)
#             db_session.commit()

from model.User_model import UserModel
from public.db import db_session


class UserMapper:
    @classmethod
    def save(cls, user_body: UserModel):  # user表添加
        db_session.add(user_body)
        db_session.commit()

    @classmethod
    def get_name(cls, username) -> UserModel:
        return UserModel.query.filter_by(username=username).first()

    @classmethod
    def get(cls, id) -> UserModel:
        return UserModel.query.filter_by(id=id).first()

    @classmethod
    def get_all(cls):
        return UserModel.query.all()

    @classmethod
    def delete(cls, id):
        db_session.query(UserModel).filter_by(id=id).delete()
        db_session.commit()

    @classmethod
    def update(cls, id, update_data):
        db_session.query(UserModel).filter_by(id=id).update(update_data)
        db_session.commit()
